<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- chown | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- chown</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;chown将指定文件的拥有者改为指定的用户或组，用户可以是用户名或者用户ID；组可以是组名或者组ID；文件是以空格分开的要改变权限的文件列表，支持通配符。系统管理员经常使用chown命令，在将文件拷贝到另一个用户的名录下之后，让用户拥有使用该文件的权限。 </p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1.命令格式"></a>1.命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chown [选项] [所有者]:[组] [文件]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2.命令功能"></a>2.命令功能</h2><h3 id="必要参数"><a href="#必要参数" class="headerlink" title="必要参数"></a>必要参数</h3><ul>
<li>-c 显示更改的部分的信息</li>
<li>-f 忽略错误信息</li>
<li>-h 修复符号链接</li>
<li>-R 处理指定目录以及其子目录下的所有文件</li>
<li>-v 显示详细的处理信息</li>
<li>-deference 作用于符号链接的指向，而不是链接文件本身</li>
</ul>
<h3 id="选择参数"><a href="#选择参数" class="headerlink" title="选择参数"></a>选择参数</h3><ul>
<li>–reference=&lt;目录或文件&gt; 把指定的目录/文件作为参考，把操作的文件/目录设置成参考文件/目录相同拥有者和群组</li>
<li>–from=&lt;当前用户：当前群组&gt; 只有当前用户和群组跟指定的用户和群组相同时才进行改变</li>
<li>–help 显示帮助信息</li>
<li>–version 显示版本信息</li>
</ul>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4.使用实例"></a>4.使用实例</h2><h3 id="实例1-改变拥有者和群组"><a href="#实例1-改变拥有者和群组" class="headerlink" title="实例1.改变拥有者和群组"></a>实例1.改变拥有者和群组</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chown mail:mail log2012.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">---xr--r-- 1 root users 302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 root users 302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 root users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment"># chown mail:mail log2012.log </span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">---xr--r-- 1 root users 302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 mail mail  302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 root users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将文件 log2012.log 的属主 root 和 属组 users 改变为 属主 mail 和属组 mail</p>
<h3 id="实例2-改变文件拥有者和群组"><a href="#实例2-改变文件拥有者和群组" class="headerlink" title="实例2.改变文件拥有者和群组"></a>实例2.改变文件拥有者和群组</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chown root: log2012.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root users 302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 mail mail  302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 root users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment"># chown root: log2012.log </span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root users 302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 root root  302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 root users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将文件 log2012.log 的属主和属组改变为 root</p>
<h3 id="实例3-改变文件群组"><a href="#实例3-改变文件群组" class="headerlink" title="实例3.改变文件群组"></a>实例3.改变文件群组</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chown :mail log2012.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root users 302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 root root  302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 root users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment"># chown :mail log2012.log </span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root users 302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 root mail  302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 root users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root users      0 11-30 08:39 log2017.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只把文件 log2012.log 的属组改变为 mail 属主不变</p>
<h3 id="实例4-改变指定目录以及其字目录下的所有文件的拥有者和群组"><a href="#实例4-改变指定目录以及其字目录下的所有文件的拥有者和群组" class="headerlink" title="实例4.改变指定目录以及其字目录下的所有文件的拥有者和群组"></a>实例4.改变指定目录以及其字目录下的所有文件的拥有者和群组</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chown -R -v root:mail test6</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 2 root users   4096 11-30 08:39 test6</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># chown -R -v root:mail test6</span></div><div class="line">“test6/log2014.log” 的所有者已更改为 root:mail</div><div class="line">“test6/linklog.log” 的所有者已更改为 root:mail</div><div class="line">“test6/log2015.log” 的所有者已更改为 root:mail</div><div class="line">“test6/log2013.log” 的所有者已更改为 root:mail</div><div class="line">“test6/log2012.log” 的所有者已保留为 root:mail</div><div class="line">“test6/log2017.log” 的所有者已更改为 root:mail</div><div class="line">“test6/log2016.log” 的所有者已更改为 root:mail</div><div class="line">“test6” 的所有者已更改为 root:mail</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 2 root mail   4096 11-30 08:39 test6</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test6</span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root mail 302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 root mail 302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 root mail     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root mail      0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root mail      0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root mail      0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root mail      0 11-30 08:39 log2017.log</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/30. Linux 命令- chown/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
